//
//  AFHTTPSessionManager+ConfigSessionManager.m
//  AFNetworking
//
//  Created by yons on 27.9.20.
//

#import "AFHTTPSessionManager+ConfigSessionManager.h"

static AFHTTPSessionManager *sessionManager = nil;
static dispatch_once_t onceToken = 0;

@implementation AFHTTPSessionManager (ConfigSessionManager)

+ (instancetype)shareSessionManager {
    dispatch_once(&onceToken, ^{
        sessionManager = [[AFHTTPSessionManager alloc] init];
        sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
        sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
        sessionManager.requestSerializer.timeoutInterval = 8.0f;
        sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:
                                                                    @"text/html",
                                                                    @"text/json",
                                                                    @"text/plain",
                                                                    @"application/json",
                                                                    @"text/javascript",
                                                                    nil];
    });
    return sessionManager;
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [AFHTTPSessionManager shareSessionManager];
}

- (id)mutableCopyWithZone:(struct _NSZone *)zone {
    return [AFHTTPSessionManager shareSessionManager];
}

- (void)destorySessionManager {
    onceToken = 0;
    sessionManager = nil;
}

#pragma mark -----------------------🍺🍺🍺----------------------- 请求头信息 -----------------------🍺🍺🍺-----------------------

- (void)setHeaderParamsDic:(NSDictionary *)headerParamsDic {
   for (NSString *key in headerParamsDic.allKeys) {
        [sessionManager.requestSerializer setValue:headerParamsDic[key] forHTTPHeaderField:key];
    }
}

- (NSDictionary *)headerParamsDic {
    return [NSDictionary dictionary];
}

#pragma mark -----------------------🍺🍺🍺----------------------- 封装数据请求 -----------------------🍺🍺🍺-----------------------

/// 封装数据请求类
/// @param url url地址
/// @param method 请求方式
/// @param parameters 请求参数
/// @param success 成功回调
/// @param failure 失败回调
- (NSURLSessionDataTask *)requestWithURL:(NSString *)url
                              httpMethod:(HttpMethod)method
                              parameters:(nullable id)parameters
                                progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                                 success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                 failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure {
    NSURLSessionDataTask *task;
    switch (method) {
        case HttpMethodGET:
            [sessionManager GET:url parameters:parameters headers:self.headerParamsDic progress:downloadProgress success:success failure:failure];
            break;
            
        case HttpMethodPOST:
            [sessionManager POST:url parameters:parameters headers:self.headerParamsDic progress:downloadProgress success:success failure:failure];
            break;
            
        case HttpMethodPUT:
            [sessionManager PUT:url parameters:parameters headers:self.headerParamsDic success:success failure:failure];
            break;
            
        case HttpMethodDELETE:
            [sessionManager DELETE:url parameters:parameters headers:self.headerParamsDic success:success failure:failure];
            break;
            
        default:
            break;
    }
    [task resume];
    return task;
}


/// 封装图片上传请求类
/// @param url url地址
/// @param parameters 参数
/// @param imageArray 图片数组
/// @param name 指定名称
/// @param success 成功回调
/// @param failure 失败回调
- (NSURLSessionDataTask *)uploadImageWithURL:(NSString *)url
                                  parameters:(nullable id)parameters
                                  imageArray:(NSArray *)imageArray
                                        name:(NSString *)name
                                    progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                                     success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                     failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure {
    NSURLSessionDataTask *task;
    [sessionManager POST:url parameters:parameters headers:self.headerParamsDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [imageArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSData *imgData = UIImageJPEGRepresentation(obj, 0.7);
            [formData appendPartWithFileData:imgData name:name fileName:[NSString stringWithFormat:@"%ld.png", idx] mimeType:@"image/png"];
        }];
    } progress:downloadProgress success:success failure:failure];
    [task resume];
    return task;
}

@end
